package com.example.flshtarvel.service;

import com.example.flshtarvel.entity.*;
import com.example.flshtarvel.mapper.*;
import com.example.flshtarvel.util.RemoveDuplicate;
import com.example.flshtarvel.vo.RouteVo;


import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RouteService {
    @Autowired(required = false)
    RouteMapper routeMapper;
    @Autowired
    ArticleService articleService;
    @Autowired
    TagService tagService;
    @Autowired(required = false)
    TagMapper tagMapper;
    @Autowired(required = false)
    ArticleRouteMapService articleRouteMapService;
    @Autowired(required = false)
    RouteTagMapService routeTagMapService;
    @Autowired
    TagArticleMapService tagArticleMapService;
    @Autowired(required = false)
    TagArticleMapMapper tagArticleMapMapper;
    @Autowired
    ImageService imageService;
    @Autowired(required = false)
    RouteTagMapMapper routeTagMapMapper;
    @Autowired(required = false)
    ArticleRouteMapMapper articleRouteMapMapper;

    public int insertRoute(RouteVo route){
        route.setRouteId(UUID.randomUUID().toString());
        /**
         * 处理行程字符串
         * */
        String scheduleString = route.getSchedule();
        //行程字符串数组
        List<String> schduleList = new ArrayList<String>();
        String[] schduleArr = scheduleString.split(";");
        for(String schduleArrItem : schduleArr){
            String[] articleIds =  schduleArrItem.split(",");
            for(String articleId : articleIds){
                //向article_route_map表插入数据
                if(articleId.isEmpty()){
                    continue;
                }
                articleRouteMapService.insertArticleRouteMap(articleId,route.getRouteId());
            }
        }
        /**
         * 处理标签字符串
         * */
        String[] tagIdArr = route.getTagId().split(";");
        for(String tagId : tagIdArr){
            if(tagId.isEmpty()){
                continue;
            }
            routeTagMapService.insertRouteTagMap(route.getRouteId(),tagId);
        }

        return routeMapper.insertRoute(route);
    }

    public RouteVo selectRouteById(String routeId){
        RouteVo routeVo = routeMapper.selectRouteById(routeId);
        /**
         * 处理行程字符串
         * */
        //行程字符串
        String scheduleString = routeVo.getSchedule();
        System.out.println("行程字符串--------------"+scheduleString);
        //单日行程
        String[] schduleArr = scheduleString.split(";");
        List<List<Article>> scheduleList = new ArrayList<List<Article>>();
        for(String schduleArrItem : schduleArr){//单日行程
            //信息（酒店，美食，景点）数组
            if(schduleArrItem.isEmpty()){
                continue;
            }
            System.out.println("单日行程"+schduleArrItem);
            String[] arrticleArr = schduleArrItem.split(",");
            List<Article> articleList = new ArrayList<Article>();
            for(String arrticleId : arrticleArr){
                if(arrticleId.isEmpty()){
                    continue;
                }
                articleList.add(articleService.selectArticleById(arrticleId));
            }
            scheduleList.add(articleList);
        }
        routeVo.setScheduleList(scheduleList);
        /**
         * 处理标签字符串
         * */
        String[] tagArr = routeVo.getTagId().split(";");
        List<Tag> tagList = new ArrayList<Tag>();
        for(String tagItem : tagArr){
            if(tagItem==""||tagItem==null){
                continue;
            }
            tagList.add(tagService.selectTagById(tagItem));
        }
        routeVo.setTagList(tagList);
        return routeVo;
    }

    public List<RouteVo> selectAllRoute(int page,int size){
        int offset = (page-1)*size;
        return routeMapper.selectAllRoute(offset,size);
    }

    public int selectRouteCount(){
        return routeMapper.selectRouteCount();
    }

    public List<RouteVo> selectRouteByName(String routeName){
        String likeSql = "%" + routeName + "%";
        return routeMapper.selectRouteByName(likeSql);
    }

    public int updateRoute(Route route,String articleIds){
        //处理删除的文章链接id
        String[] articleArr = articleIds.split(";");
        for(String articleId : articleArr){
            if(articleId.isEmpty()){
                continue;
            }
            articleRouteMapService.deleteArticleRouteMap(articleId,route.getRouteId());
        }

        //删除原有标签
        routeTagMapService.deleteRouteTagMApByRouteId(route.getRouteId());
        //处理标签
        String[] tagIdArr = route.getTagId().split(";");
        for(String tagId : tagIdArr){
            if(tagId.isEmpty()){
                continue;
            }
            routeTagMapService.insertRouteTagMap(route.getRouteId(),tagId);
        }

        return routeMapper.updateRoute(route);
    }

    public int deleteRoute(String routeId){
        articleRouteMapService.deleteArticleRouteMapByRouteId(routeId);
        return routeMapper.deleteRoute(routeId);
    }

    //首页搜索路线
    public List<RouteVo> searchRoute(String search){
        search = "%" + search + "%";
        List<RouteVo> routeVoList = new ArrayList<RouteVo>();
        //根据路线名称查
        routeVoList = routeMapper.selectRouteByName(search);
        //根据标签名称查文章
        List<Tag> tagList = tagMapper.selectTagByName(search);
        //遍历tagList,查出与tagId关联的Route
        for(Tag tag : tagList){
            routeVoList.addAll( routeTagMapService.selectRouteTagMapByTagId(tag.getTagId()));
        }
        //遍历tagList，查出与tagId关联的article
        List<TagArticleMap> tagArticleMapList = new ArrayList<TagArticleMap>();
        for(Tag tag:tagList){
            tagArticleMapList.addAll(tagArticleMapService.selectArticleByTagId(tag.getTagId()));
        }
        //遍历articleList，查出与article关联的route
        for(TagArticleMap tagArticleMap : tagArticleMapList){
            routeVoList.addAll(articleRouteMapService.seleteArticleRouteMapByArticleId(tagArticleMap.getArticleId()));
        }
        //根据文章名称查
        ArrayList<Article> articleList = new ArrayList<Article>();
        articleList = (ArrayList<Article>) articleService.selectAllArticleByName(search);
        for(Article article : articleList){
            routeVoList.addAll(articleRouteMapService.seleteArticleRouteMapByArticleId(article.getArticleId()));
        }
        //routeVoList去重
        //routeVoList = new ArrayList (new HashSet (routeVoList));
        RemoveDuplicate removeDuplicate = new RemoveDuplicate();
        ArrayList<RouteVo> routeVoList2 = removeDuplicate.removeDuplicate_2((ArrayList) routeVoList);
        //routeVoList = RemoveDuplicate.removeDuplicate_2((ArrayList) routeVoList);
        for(RouteVo r:routeVoList2){
            System.out.println(r.getRouteId());
        }




        //routeVoList添加图片
        for(RouteVo routeVo : routeVoList2){
            //单日行程数组
            String[] schedule =  routeVo.getSchedule().split(";");
            //文章id数组
            for(String scheduleString : schedule){
                String[] scheduleItem = scheduleString.split(",");
                for(String articleId : scheduleItem){
                    if(articleId.isEmpty()){
                        continue;
                    }
                    routeVo.setImageList(imageService.selectImageById(articleId));
                    break;
                }

            }
        }


        return routeVoList2;
    }


    //匹配路线  根据标签
    public List<RouteVo> matchRouteByTag(String tagString){
        List<RouteVo> routeVoList = new ArrayList<>();
        //标签数组
        String[] tagArr = tagString.split(";");
        for(String tagId : tagArr){
            //route_tag_map 匹配
            List<RouteVo> routeResult = routeTagMapMapper.selectRouteTagMapByTagId(tagId);
            for(RouteVo routeVo:routeResult){
                int index = routeVoList.indexOf(routeVo);
                if(index>=0){//结果集已有该记录，匹配度+1
                    RouteVo routeVo1 = routeVoList.get(index);
                    routeVo1.gradeAdd();
                    routeVoList.set(index,routeVo1);
                }else{//结果集没有该记录，存进结果集
                    routeVoList.add(routeVo);
                }
            }
            //tag_article_map匹配
            List<TagArticleMap> articleResult = tagArticleMapMapper.selectArticleByTagId(tagId);
            //article_routeMap匹配
            for(TagArticleMap tagArticleMap:articleResult){
                List<RouteVo> routeResult2 = new ArrayList<>();//暂存数据库操作结果
                routeResult2 = articleRouteMapMapper.seleteArticleRouteMapByArticleId(tagArticleMap.getArticleId());
                for(RouteVo routeVo:routeResult2){
                    int index = routeVoList.indexOf(routeVo);
                    if(index>=0){//结果集已有该记录，匹配度+1
                        RouteVo routeVo1 = routeVoList.get(index);
                        routeVo1.gradeAdd();
                        routeVoList.set(index,routeVo1);
                    }else{//结果集没有该记录，存进结果集
                        routeVoList.add(routeVo);
                    }
                }
            }
            //排序
            Collections.sort(routeVoList);
            //routeVoList添加图片
            for(RouteVo routeVo : routeVoList){
                //单日行程数组
                String[] schedule =  routeVo.getSchedule().split(";");
                //文章id数组
                for(String scheduleString : schedule){
                    String[] scheduleItem = scheduleString.split(",");
                    for(String articleId : scheduleItem){
                        if(articleId.isEmpty()){
                            continue;
                        }
                        routeVo.setImageList(imageService.selectImageById(articleId));
                        break;
                    }

                }
            }

            return routeVoList;

        }



        return routeVoList;
    }
   //查询所有路线    根据时间排序
    public List<RouteVo> selectRouteByTime(int page, int size){
        int offset = (page-1)*size;
        List<RouteVo> routeVoList = new ArrayList<>();
        routeVoList = routeMapper.selectRouteByTime(offset,size);
        if(routeVoList.size()!=0){
            //routeVoList添加图片,标签
            for(RouteVo routeVo : routeVoList){
                //单日行程数组
                String[] schedule =  routeVo.getSchedule().split(";");
                ArrayList<Tag> tagList = new ArrayList<>();
                tagList.addAll(routeTagMapService.selectRouteTagMapByRouteId(routeVo.getRouteId()));
                //文章id数组
                for(String scheduleString : schedule){
                    String[] scheduleItem = scheduleString.split(",");
                    for(String articleId : scheduleItem){
                        if(articleId.isEmpty()){
                            continue;
                        }
                        if(routeVo.getImageList()==null){
                            routeVo.setImageList(imageService.selectImageById(articleId));
                        }
                        tagList.addAll(tagArticleMapService.selectTagByArticleId(articleId));
                    }
                }
                //标签集合去重
                RemoveDuplicate removeDuplicate = new RemoveDuplicate();
                ArrayList<Tag> tags = removeDuplicate.removeDuplicate_2((ArrayList) tagList);
                routeVo.setTagList(tags);
            }
        }


        return routeVoList;
    }

    //查询所有路线    根据时间排序
    public List<RouteVo> selectRouteByHot(int page, int size){
        int offset = (page-1)*size;
        List<RouteVo> routeVoList = new ArrayList<>();
        routeVoList = routeMapper.selectRouteByHot(offset,size);
        if(routeVoList.size()!=0){
            //routeVoList添加图片,标签
            for(RouteVo routeVo : routeVoList){
                //单日行程数组
                String[] schedule =  routeVo.getSchedule().split(";");
                ArrayList<Tag> tagList = new ArrayList<>();
                tagList.addAll(routeTagMapService.selectRouteTagMapByRouteId(routeVo.getRouteId()));
                //文章id数组
                for(String scheduleString : schedule){
                    String[] scheduleItem = scheduleString.split(",");
                    for(String articleId : scheduleItem){
                        if(articleId.isEmpty()){
                            continue;
                        }
                        if(routeVo.getImageList()==null){
                            routeVo.setImageList(imageService.selectImageById(articleId));
                        }
                        tagList.addAll(tagArticleMapService.selectTagByArticleId(articleId));
                    }
                }
                //标签集合去重
                RemoveDuplicate removeDuplicate = new RemoveDuplicate();
                ArrayList<Tag> tags = removeDuplicate.removeDuplicate_2((ArrayList) tagList);
                routeVo.setTagList(tags);
            }
        }


        return routeVoList;
    }
}
